import $ from 'jQuery';
(function () {
    /**
     * 删除
     * @param params
     * @private
     */
    var _deleteAccount = function (params) {
        layer.confirm('&nbsp;&nbsp; &nbsp;&nbsp;你确定要刪除吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){

            IOT.showOverlay('刪除中，请稍等...');
            console.log(params);
            IOT.getServerData(URI.OPERATE.LIGHTMETHOD.DELETE, {strategyId: params.strategyId}, (ret) => {
                IOT.hideOverlay();
                if (ret && ret.code === 200) {
                    IOT.tips('刪除成功！', 'success');
                    layer.closeAll();
                    M.Table.refresh.all();
                } else {
                    IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                }
            });
        }, function(){
            //取消
        });

    };

    let $accountBox = null;
    let $vue = new Vue({
        el: '.main-hook',
        data: {
            boxs: {
                box1: {show: true, hook: '.box1-hook', lock: true, url: '', params: {}},
                box2: {show: false, hook: '.box2-hook', url: '', params: {}},
                box3: {show: false, hook: '.box3-hook', url: '', params: {}},
                box4: {show: false, hook: '.box4-hook', url: '', params: {}}
            },
            roleList: []
        },
        created: function () {
            M.Request.getRoleList(true, (roleList, ret) => {
                this.roleList = roleList;
            });
        },
        methods: {
            emitPage: function () {
                M.Page.load(this.boxs);
            },
            // 上一页
            prePage: function () {
                M.Page.prePage(this.boxs);
            },
            searchKey:function(){
                $('.search-hook').trigger('click');
            },
            // 触发刷新页(当前显示的页面)
            refreshPage: function () {
                M.Page.refreshPage(this.boxs);
            },
            // 新建照明策略
            addMethod: function (companyType) {
                //按经纬度设置 按时间节点设置
                    M.Page.emit(URI.OPERATE.LIGHTMETHOD.CREATE.PAGE);
            }
        },
        mounted: function () {
            let thiz = this;
            this.$nextTick(() => {
                let tableHookName = '.lightmethod-table-hook';
                $accountBox = $('.account-hook');
                var $form = $accountBox.find('.form-hook');
                // 查询
                $form.off('submit').on('submit', function () {
                    $(tableHookName).bootstrapTable('onCustomSearch');
                    return false;
                });
                var $tableHook = $accountBox.find(tableHookName).bootstrapTable({
                    scrollbar: 'account-table-body',
                    striped: true, // 设置为 true 会有隔行变色效果
                    pagination: true, // true 显示分页
                    paginationDetail: true, // 分页详情
                    sidePagination: 'server', // 服务器端
                    method: 'post',
                    url: URI.OPERATE.LIGHTMETHOD.LIST,
                    debug: false,
                    cache: false,
                    pageNumber: 1,
                    pageSize: 10,
                    pageList: [10, 20, 50, 100],
                    smartDisplay:false,
                    // fixedColumns: true,
                    customButton: '',
                    customQueryParams: function (params) {
                        var formParams = $form.serializeJson();
                        var queryParams = $.extend({}, params, formParams);
                        //console.log(queryParams);
                        return queryParams;
                    },
                    onLoadSuccess: function (data) {
                        //console.log(JSON.stringify(data));
                    },
                    onLoadError: function (status, xhr) {
                    },
                    onCheckAll: function (rows) {

                    },
                    columns: [
                        {
                            field: 'strategyName', title: '照明策略名称', align: 'center',width:100,fixedLeft: true,tips:true
                        },
                        {
                            field: 'createDate', title: '创建时间', align: 'center',width:100, fixedLeft: true,tips:true
                        },
                        {
                            field: 'operate', title: '操作', align: 'center', fixedLeft: true, width: 180,
                            events: {
                                'click .detail-account': function (e, value, rowData, index) {
                                    M.Page.emit(URI.OPERATE.LIGHTMETHOD.DETAIL.PAGE);
                                    IOT.setSessionStore(URI.OPERATE.LIGHTMETHOD.DETAIL.PAGE, JSON.stringify(rowData));
                                    return false;
                                },
                                'click .modify-account': function (e, value, rowData, index) {
                                    M.Page.emit(URI.OPERATE.LIGHTMETHOD.MODIFY.PAGE);
                                    IOT.setSessionStore(URI.OPERATE.LIGHTMETHOD.MODIFY.PAGE, JSON.stringify(rowData));
                                    return false;
                                },
                                'click .delete-account': function (e, value, row, index) {
                                    _deleteAccount(row);
                                    return false;
                                },

                            },
                            formatter: function (value, row, index) {
                                let html = [];
                                if (M.Authority.checkAuthority('account-detail')) {
                                    html.push('<a class="detail-account operate">详细</a>');
                                }
                                if (M.Authority.checkAuthority('account-modify')) {
                                    html.push('<a class="modify-account operate">修改</a>');
                                }
                                if (!row.admin) {
                                    if(row.strategyFlag!="0"){
                                        if (M.Authority.checkAuthority('account-delete')) {
                                            html.push('<a class="delete-account operate">删除</a>');
                                         }
                                    }
                                }
                                return html.join('');
                            }
                        }
                    ]
                });
                //权限——查看
                if (!M.Authority.checkAuthority('account-detail')&&!M.Authority.checkAuthority('account-modify')) {
                    $(tableHookName).bootstrapTable('hideColumn', 'operate');//隐藏列
                }
            });
        }
    });
})();